package com.pzhu.user.dao;

import static com.pzhu.user.domain.entity.table.PermissionTableDef.PERMISSION;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.pzhu.base.common.utils.mybatisflex.BaseService;
import com.pzhu.base.common.utils.mybatisflex.LogicDeleteService;
import com.pzhu.base.common.utils.mybatisflex.QueryUtils;
import com.pzhu.user.domain.entity.Permission;
import com.pzhu.user.domain.ro.PermissionSearchRO;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

/**
 * @author 75073
 * @description 针对表【permission(权限表)】的数据库操作Mapper
 * @createDate 2024-02-19 00:37:22
 */
public interface PermissionDAO extends LogicDeleteService<Permission> {

    default Page<Permission> search(PermissionSearchRO permissionSearchRO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        QueryCondition queryCondition = QueryUtils.or(
                PERMISSION.ID.eq(permissionSearchRO.getId()),
                PERMISSION.CODE.like(permissionSearchRO.getCode()),
                PERMISSION.PERMISSION_DESC.like(permissionSearchRO.getDesc()));
        queryWrapper.where(queryCondition);
        return page(permissionSearchRO, queryWrapper);
    }

    default Permission getByPermissionId(Long permissionId) {
        if (Objects.isNull(permissionId)) {
            return null;
        }
        return getById(permissionId);
    }

    default boolean existCode(String code) {
        if (StringUtils.isNotBlank(code)) {
            return false;
        }
        return exists(QueryUtils.and(PERMISSION.CODE.eq(code), QueryUtils.del(PERMISSION.DELETED_ID)));
    }
}
